package index;

import java.io.File;

import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class FirstSearch {

	public static void main(String[] args) throws Exception {
		
		String indexDir = "/Users/apple/lucene/index";
		String keyword = "涂有";
		search(indexDir, keyword);
	}
	
	public static void search(String indexDir, String keyword) throws Exception{
		
		Directory dir = FSDirectory.open(new File(indexDir));
		QueryParser parser = new QueryParser(Version.LUCENE_33, "文档", new IKAnalyzer());
		Query query = parser.parse(keyword);
		long start = System.currentTimeMillis();
		IndexSearcher search = new IndexSearcher(dir);
		TopDocs docs = search.search(query, 10);
		long end = System.currentTimeMillis();
		System.out.println("发现:" + docs.totalHits + "文档,共花费：" + (end - start) + "毫秒和字符：" + keyword + "匹配");
		for(ScoreDoc doc : docs.scoreDocs){
			
			System.out.println(search.doc(doc.doc).get("路径") + "--" + doc.score);
		}
	}
}
